package com.framework.modules.YhbUser.serivce.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.framework.dao.YHBDao.user.YhbCusTerDao;
import com.framework.exception.RRException;
import com.framework.modules.YhbUser.serivce.YhbCustomerService;
import com.framework.page.PageUtils;
import com.framework.service.yhbservice.YhbTerminalService;
import com.framework.service.yhbservice.YhbUserService;
import com.framework.service.yhbservice.YhbUserTerminalRelationService;
import com.framework.yhbEntity.user.YhbCusTer;
import com.framework.yhbEntity.user.YhbUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service("yhbCustomerService")
public class YhbCustomerServiceImpl extends ServiceImpl<YhbCusTerDao, YhbCusTer> implements YhbCustomerService {

    @Autowired
    private YhbUserTerminalRelationService yhbUserTerminalRelationService;

    @Autowired
    private YhbUserService yhbUserService;

    @Autowired
    private YhbTerminalService yhbTerminalService;

    @Override
    public PageUtils queryCustomerPage(Map<String, Object> params) {
        Integer currpage = Integer.valueOf(params.get("page").toString());
        Integer pageSize = Integer.valueOf(params.get("limit").toString());
        params.put("start", (currpage - 1) * pageSize);
        params.put("limit", pageSize);
        List<Map<String,Object>> list=baseMapper.queryCustomerlist(params);
        Integer count=baseMapper.queryCustomerCount(params);
        return new PageUtils(list, count, pageSize, currpage);
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void exmaineCuster(String yhbCusTerId, Integer state) {
        if(state.intValue()==1){
            //修改商户信息
            YhbCusTer yct =setState(yhbCusTerId,state);

            //注册用户
            YhbUser user=yhbUserService.addNewUser(yct);

            //添加关联
            yhbUserTerminalRelationService.addRelation(yct,user);

            yhbTerminalService.setIsYhbUse(yct.getSn());


        }else if(state.intValue()==3) {
            setState(yhbCusTerId,state);
        }else {
            throw new RRException("9000","审核状态有误");
        }

    }
    @Transactional(rollbackFor = Exception.class)
    private YhbCusTer setState(String yhbCusTerId,Integer state) {
        YhbCusTer t = baseMapper.selectById(yhbCusTerId);

        if(t==null){
            throw new RRException("9000","未查到用户信息");
        }
        t.setState(state);
        baseMapper.updateById(t);
        return  baseMapper.selectById(yhbCusTerId);
    }
}
